home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / kermit / b / ckvcon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-09  |  29.5 KB  |  936 lines

  1. #ifndef VMS
  2.       ERROR -- CKVCON.C is used only on the OpenVMS(tm) Operating System
  3. #endif /* VMS */
  4. #define CONN_OS_ARCH_STRING \
  5.         "Connect Command 5A(029) 11 Oct 92";
  6. #ifdef __ALPHA
  7.          /* do nothing */
  8. #else
  9. # ifdef VAX
  10. #  module ckvcon "5.0-029"
  11. # else
  12. #ifndef __GNUC__
  13.       ERROR -- CKVCON.C unknown architecture, neither VAX(tm) nor Alpha
  14. # endif /* __GNUC__ */
  15. # endif /* VAX */
  16. #endif /* __ALPHA */
  17.  
  18. char *connv = CONN_OS_ARCH_STRING;
  19.  
  20. /*  C K V C O N  --  Dumb terminal connection to remote system, for VMS  */
  21. /*
  22.   Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  23.   Columbia University Academic Information Systems, New York City.
  24.  
  25.   Copyright (C) 1985, 1993, Trustees of Columbia University in the City of New
  26.   York.  The C-Kermit software may not be, in whole or in part, licensed or
  27.   sold for profit as a software product itself, nor may it be included in or
  28.   distributed with commercial products or otherwise distributed by commercial
  29.   concerns to their clients or customers without written permission of the
  30.   Office of Kermit Development and Distribution, Columbia University.  This
  31.   copyright notice must not be removed, altered, or obscured.
  32. */
  33. /*
  34.  * Orignally adapted from the UNIX C-Kermit CONNECT module by S. Rubenstein
  35.  * for systems without fork().  This version of conect() uses contti(&c, &src)
  36.  * to return when a character is available from either the console or the comm
  37.  * line, to allow sending/receiving without breaking connection.
  38.  *
  39.  * Edit by
  40.  *
  41.  * 010 06-Mar-1989 mab    General cleanup
  42.  * 011 23-Mar-1989 mab    Clean up doesc() code.  Add malloc() in place of
  43.  *             static buffer space.  Also increase buffer space.
  44.  * 012 27-Sep-1989 mab    Add XON sequence.
  45.  * 013 30-Mar-1991 fdc  Add so/si, character set translation.
  46.  * 014 06-Apr-1991 fdc  Adapted for TGV MultiNet TCP/IP connections.
  47.  * 015 21-Jun-1991 tmk  Cleaned up typo in session logging display.
  48.  * 016 28-Nov-1991 fdc  "Back at <hostname>", disallow CONNECT in background
  49.  * 017 25-Dec-1991 fdc  Added support for NOPUSH, added "Hanging up" message
  50.  * 018 11-Jan-1992 fdc  Added support for key mapping
  51.  * 019 27-Jan-1992 fdc  Added support for ANSI escape sequence recognition
  52.  * 020 00-May-1992 fdc  Rewrote conect() to used buffered i/o.
  53.  * 021 10-Jun-1992 fdc  Added support for Wollongong WIN/TCP from Ray Hunter.
  54.  *                      Fix messed-up help message.
  55.  * 022 20-Jun-1992 fdc  Fixed handling of CR on TELNET sessions.
  56.  * 023 28-Jun-1992 fdc  Cosmetic cleanup of "Connecting..." message.
  57.  * 024 12-Jul-1992 fdc  Added mdmhup() feature (see ckudia.c).
  58.  * 025 29-Jul-1992 fdc  Grouped TELNET IP and AYT into single writes.
  59.  * 026 13-Aug-1992 fdc  Added support for SET TELNET NEWLINE-MODE.
  60.  * 027 05-Sep-1992 lt   Added architecture ifdefs for OpenVMS, Alpha, at top.
  61.  * 028 08-Sep-1992 fdc  Separated input and output SO/SO shift states.
  62.  * 029 11-Oct-1992 fdc  Reduce modem-vs-net confusion in ttopen() calls.
  63.  */
  64.  
  65. #include "ckcdeb.h"
  66. #include "ckcasc.h"
  67. #include "ckcker.h"
  68. #include "ckcnet.h"
  69. #include "ckvvms.h"
  70. #ifndef NOCSETS
  71. #include "ckcxla.h"            /* Character set translation */
  72. #endif /* NOCSETS */
  73. #include <stdio.h>
  74. #include <ctype.h>
  75. #include <signal.h>
  76. #include <setjmp.h>
  77.  
  78. static int src;                /* Where input character came from */
  79.  
  80. extern int local, speed, escape, duplex, parity, flow, seslog, mdmtyp, batch;
  81. extern int cmask, cmdmsk, debses, sosi, ttyfd, what, quiet, backgrd, tnlm,
  82.  tt_crd, tn_nlm;
  83. extern char ttname[], sesfil[], myhost[];
  84.  
  85. #ifndef NOICP                /* Keyboard mapping */
  86. #ifndef NOSETKEY
  87. extern KEY *keymap;            /* Single-character key map */
  88. extern MACRO *macrotab;            /* Key macro pointer table */
  89. static MACRO kmptr = NULL;        /* Pointer to current key macro */
  90. #endif /* NOSETKEY */
  91. #endif /* NOICP */
  92.  
  93. /* Network support */
  94. extern int ttnproto,            /* Virtual terminal protocol */
  95.   network,                /* Network connection active */
  96.   nettype;                /* Network type */
  97. #ifdef TNCODE
  98. /* Telnet-only variables */
  99. extern int tn_init;            /* Telnet initialized flag */
  100. #endif /* TNCODE */
  101.  
  102. #ifndef NOCSETS
  103. #ifdef CK_ANSIC
  104. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  105. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  106. static CHAR (*sxo)(CHAR);    /* Local translation functions */
  107. static CHAR (*rxo)(CHAR);    /* for output (sending) terminal chars */
  108. static CHAR (*sxi)(CHAR);    /* and for input (receiving) terminal chars. */
  109. static CHAR (*rxi)(CHAR);
  110. #else
  111. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])();    /* Character set */
  112. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])();    /* translation functions. */
  113. static CHAR (*sxo)();        /* Local translation functions */
  114. static CHAR (*rxo)();        /* for output (sending) terminal chars */
  115. static CHAR (*sxi)();        /* and for input (receiving) terminal chars. */
  116. static CHAR (*rxi)();
  117. #endif /* CK_ANSIC */
  118. extern int language;        /* Current language. */
  119. extern struct csinfo fcsinfo[]; /* File character set info */
  120. extern int tcsr, tcsl;        /* Terminal character sets, remote & local. */
  121. static int langsv;        /* Remember language */
  122. static int tcs;            /* Intermediate (xfer) char set */
  123.  
  124. _PROTOTYP( VOID doesc, (unsigned char) );
  125.  
  126. #ifndef NOESCSEQ
  127. /*
  128.   As of edit 178, the CONNECT command will skip past ANSI escape sequences
  129.   to avoid translating the characters within them.  This allows the CONNECT
  130.   command to work correctly when connected to a remote host that uses a
  131.   7-bit ISO 646 national character set, in which characters like '[' would
  132.   normally be translated into accented characters, ruining the terminal's
  133.   interpretation (and generation) of escape sequences.
  134.  
  135.   Escape sequences of non-ANSI/ISO-compliant terminals are not handled.
  136. */
  137. #ifndef SKIPESC
  138. #define SKIPESC
  139. #endif /* SKIPESC */
  140. /*
  141.   States for the escape-sequence recognizer.
  142. */
  143. #define ES_NORMAL 0            /* Normal, not in escape sequence */
  144. #define ES_GOTESC 1            /* Current character is ESC */
  145. #define ES_ESCSEQ 2            /* Inside an escape sequence */
  146. #define ES_GOTCSI 3            /* Inside a control sequence */
  147. #define ES_STRING 4            /* Inside OSC, PM, or APC string */
  148. #define ES_TERMIN 5            /* 1st char of string terminator */
  149.  
  150. static int
  151.   skipesc = 0,                /* Skip over ANSI escape sequences */
  152.   inesc = ES_NORMAL;            /* State of sequence recognizer */
  153. /*
  154.   ANSI escape sequence handling.  Only the 7-bit form is treated, because
  155.   translation is not a problem in the 8-bit environment, in which all GL
  156.   characters are ASCII and no translation takes place.  So we don't check
  157.   for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.  Here
  158.   is the ANSI sequence recognizer state table, followed by the code that
  159.   implements it.
  160.  
  161.   Definitions:
  162.     CAN = Cancel                       01/08         Ctrl-X
  163.     SUB = Substitute                   01/10         Ctrl-Z
  164.     DCS = Device Control Sequence      01/11 05/00   ESC P
  165.     CSI = Control Sequence Introducer  01/11 05/11   ESC [
  166.     ST  = String Terminator            01/11 05/12   ESC \
  167.     OSC = Operating System Command     01/11 05/13   ESC ]
  168.     PM  = Privacy Message              01/11 05/14   ESC ^
  169.     APC = Application Program Command  01/11 05/15   ESC _
  170.  
  171.   ANSI escape sequence recognizer:
  172.  
  173.     State    Input  New State  ; Commentary
  174.  
  175.     NORMAL   (start)           ; Start in NORMAL state
  176.  
  177.     (any)    CAN    NORMAL     ; ^X cancels
  178.     (any)    SUB    NORMAL     ; ^Z cancels
  179.  
  180.     NORMAL   ESC    GOTESC     ; Begin escape sequence
  181.     NORMAL   other             ; NORMAL control or graphic character
  182.  
  183.     GOTESC   ESC               ; Start again
  184.     GOTESC   [      GOTCSI     ; CSI
  185.     GOTESC   P      STRING     ; DCS introducer, consume through ST
  186.     GOTESC   ]      STRING     ; OSC introducer, consume through ST
  187.     GOTESC   ^      STRING     ; PM  introducer, consume through ST
  188.     GOTESC   _      STRING     ; APC introducer, consume through ST
  189.     GOTESC   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  190.     GOTESC   other  ESCSEQ     ; Intermediate or ignored control character
  191.  
  192.     ESCSEQ   ESC    GOTESC     ; Start again
  193.     ESCSEQ   0..~   NORMAL     ; 03/00 through 17/14 = Final character
  194.     ESCSEQ   other             ; Intermediate or ignored control character
  195.  
  196.     GOTCSI   ESC    GOTESC     ; Start again
  197.     GOTCSI   @..~   NORMAL     ; 04/00 through 17/14 = Final character
  198.     GOTCSI   other             ; Intermediate char or ignored control char
  199.  
  200.     STRING   ESC    TERMIN     ; Maybe have ST
  201.     STRING   other             ; Consume all else
  202.  
  203.     TERMIN   \      NORMAL     ; End of string
  204.     TERMIN   other  STRING     ; Still in string
  205. */
  206. /*
  207.   chkaes() -- Check ANSI Escape Sequence.
  208.   Call with EACH character in input stream.
  209.   Sets global inesc variable according to escape sequence state.
  210. */
  211. VOID
  212. #ifdef CK_ANSIC
  213. chkaes(char c)
  214. #else
  215. chkaes(c) char c;
  216. #endif /* CK_ANSIC */
  217. /* chkaes */ {
  218.  
  219.     if (c == CAN || c == SUB)        /* CAN and SUB cancel any sequence */
  220.       inesc = ES_NORMAL;
  221.     else                /* Otherwise */
  222.       switch (inesc) {            /* enter state switcher */
  223.  
  224.     case ES_NORMAL:            /* NORMAL state */
  225.       if (c == ESC)            /* Got an ESC */
  226.         inesc = ES_GOTESC;        /* Change state to GOTESC */
  227.       break;            /* Otherwise stay in NORMAL state */
  228.  
  229.     case ES_GOTESC:            /* GOTESC state */
  230.       if (c == '[')            /* Left bracket after ESC is CSI */
  231.         inesc = ES_GOTCSI;        /* Change to GOTCSI state */
  232.       else if (c > 057 && c < 0177)    /* Final character '0' thru '~' */
  233.         inesc = ES_NORMAL;        /* Back to normal */
  234.       else if (c == 'P' || (c > 0134 && c < 0140)) /* P, [, ^, or _ */
  235.         inesc = ES_STRING;        /* Switch to STRING-absorption state */
  236.       else if (c != ESC)        /* ESC in an escape sequence... */
  237.         inesc = ES_ESCSEQ;        /* starts a new escape sequence */
  238.       break;            /* Intermediate or ignored ctrl char */
  239.  
  240.     case ES_ESCSEQ:            /* ESCSEQ -- in an escape sequence */
  241.       if (c > 057 && c < 0177)    /* Final character '0' thru '~' */
  242.         inesc = ES_NORMAL;        /* Return to NORMAL state. */
  243.       else if (c == ESC)        /* ESC ... */
  244.         inesc = ES_GOTESC;        /* starts a new escape sequence */
  245.       break;            /* Intermediate or ignored ctrl char */
  246.  
  247.     case ES_GOTCSI:            /* GOTCSI -- In a control sequence */
  248.       if (c > 077 && c < 0177)    /* Final character '@' thru '~' */
  249.         inesc = ES_NORMAL;        /* Return to NORMAL. */
  250.       else if (c == ESC)        /* ESC ... */
  251.         inesc = ES_GOTESC;        /* starts over. */
  252.       break;            /* Intermediate or ignored ctrl char */
  253.  
  254.     case ES_STRING:            /* Inside a string */
  255.       if (c == ESC)            /* ESC may be 1st char of terminator */
  256.         inesc = ES_TERMIN;        /* Go see. */
  257.       break;            /* Absorb all other characters. */
  258.  
  259.     case ES_TERMIN:            /* May have a string terminator */
  260.       if (c == '\\')        /* which must be backslash */
  261.         inesc = ES_NORMAL;        /* If so, back to NORMAL */
  262.       else                /* Otherwise */
  263.         inesc = ES_STRING;        /* Back to string absorption. */
  264.       }
  265. }
  266. #endif /* NOESCSEQ */
  267. #endif /* NOCSETS */
  268.  
  269. int i, active;                /* Variables global to this module */
  270. static char *p;
  271.  
  272. static char *ibp;            /* Input buffer pointer */
  273. static int ibc = 0;            /* Input buffer count */
  274. #define IBUFL 1024            /* Input buffer length */
  275.  
  276. static char *obp;            /* Output buffer pointer */
  277. static int obc = 0;            /* Output buffer count */
  278. #define OBUFL 1024            /* Output buffer length */
  279.  
  280. #ifdef DYNAMIC
  281. static char *ibuf, *obuf;        /* Line and temp buffers */
  282. #else
  283. static char ibuf[IBUFL], obuf[OBUFL];
  284. #endif /* DYNAMIC */
  285.  
  286. char kbuf[10], *kbp;            /* Keyboard buffer */
  287.  
  288. /*  C O N E C T  --  Perform terminal connection  */
  289.  
  290. int inshift, outshift;            /* SO/SI shift states */
  291.  
  292. /*  C K C P U T C  --  C-Kermit CONNECT Put Character to Screen  */
  293. /*
  294.   Output is buffered to avoid slow screen writes on fast connections.
  295. */
  296. int
  297. ckcputf() {                /* Dump the output buffer */
  298.     int x;
  299.     if (obc > 0)            /* If we have any characters, */
  300.       x = conxo(obc,obuf);        /* dump them, */
  301.     obp = obuf;                /* reset the pointer */
  302.     obc = 0;                /* and the counter. */
  303.     return(x);                /* Return conxo's return code */
  304. }
  305.  
  306. int
  307. ckcputc(c) int c; {
  308.     int x;
  309.  
  310.     *obp++ = c & 0xff;            /* Deposit the character */
  311.     obc++;                /* Count it */
  312.     if (ibc == 0 ||            /* If input buffer empty */
  313.     obc == OBUFL) {            /* or output buffer full */
  314.     x = conxo(obc,obuf);        /* dump the buffer, */
  315.     obp = obuf;            /* reset the pointer */
  316.     obc = 0;            /* and the counter. */
  317.     return(x);            /* Return conxo's return code */
  318.     } else return(0);
  319. }
  320.  
  321. /*  C K C G E T C  --  C-Kermit CONNECT Get Character  */
  322. /*
  323.   Buffered read from communication device.
  324.   Returns the next character, refilling the buffer if necessary.
  325.   On error, returns ttinc's return code (see ttinc() description).
  326.   Dummy argument for compatible calling conventions with ttinc().
  327. */
  328. int
  329. ckcgetc(dummy) int dummy; {
  330.     int c, n;
  331.  
  332.     if (ibc > 0) {            /* Have buffered port characters */
  333.     src = 1;            /* Say source is port */
  334.     c = *ibp++ & 0xff;        /* Get next character */
  335.     ibc--;                /* Reduce input buffer count */
  336.     debug(F101,"CKCGETC returns buffered port char","",c);
  337.     return(c);            /* Return buffered port character */
  338.     } else {                /* Need to refill buffer */
  339.     contti(&c, &src);        /* Read one character */
  340.     if (src < 0) {            /* If error, return error code */
  341.         return(src);
  342.     } else if (src == 0) {        /* Got a character from the keyboard */
  343.         debug(F101,"CKCGETC returns keyboard char","",c);
  344.         return(c);
  345.     } else {            /* Got a port character */
  346.         ibp = ibuf;            /* Reset buffer pointer */
  347.         *ibp++ = c;            /* Deposit the character */
  348.         ibc++;            /* and count it */
  349.         if ((n = ttchk()) > 0) {    /* Any more characters waiting? */
  350.         if (n > (IBUFL - ibc))    /* Get them all at once. */
  351.           n = IBUFL - ibc;    /* Don't overflow the buffer */
  352.         if ((n = ttxin(n,(CHAR *)ibp)) > 0) /* Read waiting chars */
  353.           ibc += n;        /* Advance counter */
  354.         }
  355.         ibp = ibuf;            /* Reset buffer pointer again */
  356.         c = *ibp++ & 0xff;        /* Get first character from buffer */
  357.         ibc--;            /* Reduce buffer count */
  358.         debug(F101,"CKCGETC returns port char","",c);
  359.         return(c);            /* Return the character */
  360.     }
  361.     }
  362. }
  363.  
  364. int
  365. conect() {
  366.     int c;            /* c is a character, but must be signed
  367.                    integer to pass thru -1, which is the
  368.                    modem disconnection signal, and is
  369.                    different from the character 0377 */
  370.     int c2, csave;        /* Copies of c */
  371.     char errmsg[50], *erp, *cp;
  372.     int n, x;                /* Workers */
  373.  
  374.     if (!local) {
  375. #ifdef NETCONN
  376.     printf("Sorry, you must SET LINE or SET HOST first\n");
  377. #else
  378.     printf("Sorry, you must SET LINE first\n");
  379. #endif /* NETCONN */
  380.     return(-2);
  381.     }
  382.     if (batch || backgrd) {
  383.     printf(
  384. "\r\nSorry, Kermit's CONNECT command can be used only in the foreground\r\n");
  385.     return(0);
  386.     }
  387. #ifdef TCPSOCKET
  388.     if (network && (nettype != NET_TCPB)) {
  389.     printf("Sorry, network type not supported yet\n");
  390.     return(0);
  391.     }
  392. #endif /* TCPSOCKET */
  393.  
  394.     if (speed < 0 && network == 0) {
  395.     printf("Sorry, you must SET SPEED first\n");
  396.     return(-2);
  397.     }
  398.     if ((escape < 0) || (escape > 0177)) {
  399.     printf("Your escape character is not ASCII - %d\n",escape);
  400.     return(-2);
  401.     }
  402.     if (ttyfd < 0) {            /* If communication device not open */
  403.     debug(F111,"ckvcon opening",ttname,0); /* Open it now. */
  404.     if (ttopen(ttname,
  405.            &local,
  406.            network ? -nettype : mdmtyp,
  407.            0
  408.            ) < 0) {
  409.         erp = errmsg;
  410.         sprintf(erp,"Sorry, can't open %s",ttname);
  411.         perror(errmsg);
  412.         debug(F110,"ckvcon open failure",errmsg,0);
  413.         return(-2);
  414.     }
  415.     }
  416.     if (!quiet) {
  417. #ifdef NETCONN
  418.     if (network) {
  419.         printf("\nConnecting to host %s",ttname);
  420.     } else {
  421. #endif /* NETCONN */
  422.         printf("\nConnecting to %s",ttname);
  423.         if (speed > -1L) printf(", speed %ld",speed);
  424. #ifdef NETCONN
  425.     }
  426. #endif /* NETCONN */
  427.     printf(".\r\nThe escape character is %s (ASCII %d).\r\n",
  428.            dbchr(escape),escape);
  429.     printf("Type the escape character followed by C to get back,\r\n");
  430.     printf("or followed by ? to see other options.\r\n");
  431.     if (seslog) {
  432.         printf("(Session logged to %s)\r\n",sesfil);
  433.     }
  434.     if (debses) printf("Debugging Display...)\r\n");
  435.     printf("\r\n");
  436.     }
  437.  
  438. /* Condition console terminal and communication line */
  439.  
  440.     if (conbin(escape) < 0) {
  441.     printf("Sorry, can't condition console terminal\n");
  442.     return(-2);
  443.     }
  444.     if (ttvt(speed,flow) < 0) {
  445.     /* tthang(); */ /* Closing it should be quite enough! */
  446.     ttclos(0);
  447.     if (ttopen(ttname,        /* Open it again... */
  448.            &local,
  449.            network ? -nettype : mdmtyp,
  450.            0
  451.            ) < 0) {
  452.         erp = errmsg;
  453.         sprintf(erp,"Sorry, can't reopen %s",ttname);
  454.         perror(errmsg);
  455.         return(0);
  456.     }
  457.     if (ttvt(speed,flow) < 0) {    /* Try virtual terminal mode again. */
  458.         conres();            /* Failure this time is fatal. */
  459.         printf("Sorry, Can't condition communication line\n");
  460.         return(0);
  461.     }
  462.     }
  463.     debug(F101,"connect ttvt ok, escape","",escape);
  464.  
  465. #ifndef NOCSETS
  466. /* Set up character set translations */
  467.  
  468. #ifdef KANJI
  469. /* Kanji not supported yet */
  470.     if (fcsinfo[tcsr].alphabet == AL_JAPAN ||
  471.     fcsinfo[tcsl].alphabet == AL_JAPAN ) {
  472.     tcs = TC_TRANSP;
  473.     } else
  474. #endif /* KANJI */
  475. #ifdef CYRILLIC
  476.       if (fcsinfo[tcsl].alphabet == AL_CYRIL) {
  477.       tcs = TC_CYRILL;
  478.       } else
  479. #endif /* CYRILLIC */
  480.     tcs = TC_1LATIN;
  481.  
  482.     if (tcsr == tcsl) {            /* Remote and local sets the same? */
  483.     sxo = rxo = NULL;        /* If so, no translation. */
  484.     sxi = rxi = NULL;
  485.     } else {                /* Otherwise, set up */
  486.     sxo = xls[tcs][tcsl];        /* translation function */
  487.     rxo = xlr[tcs][tcsr];        /* pointers for output functions */
  488.     sxi = xls[tcs][tcsr];        /* and for input functions. */
  489.     rxi = xlr[tcs][tcsl];
  490.     }
  491. /*
  492.   This is to prevent use of zmstuff() and zdstuff() by translation functions.
  493.   They only work with disk i/o, not with communication i/o.  Luckily Russian
  494.   translation functions don't do any stuffing...
  495. */
  496.     langsv = language;
  497. #ifndef NOCYRIL
  498.     if (language != L_RUSSIAN)
  499. #endif /* NOCYRIL */
  500.       language = L_USASCII;
  501.  
  502. #ifdef SKIPESC
  503. /*
  504.   We need to activate the "skip escape sequence" feature when:
  505.   (a) translation is elected, and
  506.   (b) the local and/or remote set is 7-bit set other than US or UK ASCII.
  507. */
  508.     skipesc = (tcs != TC_TRANSP) &&    /* Not transparent */
  509.       (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  510.     (fcsinfo[tcsl].code != FC_USASCII) && /* Not US ASCII */
  511.     (fcsinfo[tcsl].code != FC_UKASCII);   /* Not UK ASCII */
  512.     inesc = ES_NORMAL;            /* Initial state of recognizer */
  513. #ifdef COMMENT
  514.     debug(F101,"tcs","",tcs);
  515.     debug(F101,"tcsl","",tcsl);
  516.     debug(F101,"tcsr","",tcsr);
  517.     debug(F101,"fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  518.     debug(F101,"fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  519. #endif /* COMMENT */
  520.     debug(F101,"skipesc","",skipesc);
  521. #endif /* SKIPESC */
  522. #endif /* NOCSETS */
  523.  
  524. #ifdef DYNAMIC
  525.     if (!(ibuf = malloc(IBUFL+1))) {    /* Allocate input line buffer */
  526.     printf("Sorry, CONNECT input buffer can't be allocated\n");
  527.     return(0);
  528.     }
  529.     if (!(obuf = malloc(OBUFL+1))) {    /* Allocate input line buffer */
  530.     printf("Sorry, CONNECT output buffer can't be allocated\n");
  531.     free(ibuf);
  532.     return(0);
  533.     }
  534. #endif /* DYNAMIC */
  535.  
  536.     inshift = outshift = 0;        /* Initial SI/SO states */
  537.     ibp = ibuf;                /* Input and output buffers */
  538.     ibc = 0;
  539.     obp = obuf;
  540.     obc = 0;
  541.     active = 1;
  542.  
  543.     do {                /* Top of big loop */
  544. #ifndef NOSETKEY
  545. /*
  546.   Before reading anything from the keyboard, continue expanding the current
  547.   active keyboard macro, if any.
  548. */
  549.     if (kmptr) {            /* Have active macro */
  550.         src = 0;            /* Pretend char came from keyboard */
  551.         if ((c = (CHAR) *kmptr++) == NUL) { /* but get it from the macro */
  552.         kmptr = NULL;        /* If no more chars in macro,  */
  553.         continue;        /* reset pointer and continue. */
  554.         }
  555.     } else                 /* OTHERWISE... */
  556. #endif /* NOSETKEY */
  557. /*
  558.   contti() samples the keyboard and the communication device, in that order,
  559.   in what amounts to a busy loop, and does not return until it has something
  560.   from one or the other.  This drives the VAX crazy, and also gives very poor
  561.   performance: input is character-at a time, and the keyboard buffer is
  562.   checked at least once for every character that comes in from the remote.
  563.   Somebody who knows something about VMS, PLEASE FIND A BETTER WAY.
  564.  
  565.   We want a version of contti() that does what select() does in BSD, and we
  566.   also want it to be buffered internally, so it doesn't have to call the
  567.   operating system for every single input character.  And most of all we don't
  568.   want it to run a busy loop all the time.  No doubt this involves ASTs and
  569.   QIOs, things of which I know nothing.
  570. */
  571.     c = ckcgetc(0);            /* Calls contti()... */
  572. /*
  573.   Get here with a character in c, and:
  574.  
  575.   src = -1 Communication error
  576.          1 Character from comm line
  577.          0 Character from console
  578. */
  579.  
  580.     if (src < 0) {            /* Comm line hangup or other error */
  581. /*
  582.   We should check WHY src < 0 and not just dive under for ANY reason.
  583. */
  584.         if (!quiet) printf("\r\nCommunications disconnect ");
  585.         active = 0;
  586.     } else if (!src) {
  587. /*
  588.    Character from console
  589. */
  590.         c &= cmdmsk;        /* Do requested masking */
  591. #ifndef NOICP
  592. #ifndef NOSETKEY
  593. /*
  594.   Note: kmptr is NULL if we got character c from the keyboard, and it is
  595.   not NULL if it came from a macro.  In the latter case, we must avoid
  596.   expanding it again.
  597. */
  598.         if (!kmptr && macrotab[c]) { /* If a macro is assigned to it */
  599.         kmptr = macrotab[c];    /* set up the pointer */
  600.         continue;        /* and do this again. */
  601.         } else c = keymap[c];    /* Else use single-char keymap */
  602. #endif /* NOSETKEY */
  603. #endif /* NOICP */
  604.         csave = c;
  605.         if (
  606. #ifndef NOICP
  607. #ifndef NOSETKEY
  608.             !kmptr &&
  609. #endif /* NOSETKEY */
  610. #endif /* NOICP */
  611.         ((c & 0x7f) == escape)) { /* Escape character? */
  612.         conresne();        /* Restore to normal attributes */
  613.         c = coninc(0) & 0177;
  614.         doesc(c);
  615.         conbin(escape);
  616.         } else {            /* Ordinary character */
  617. #ifndef NOCSETS
  618. #ifndef SKIPESC
  619.         /* Translate character sets */
  620.         if (sxo) c = (*sxo)(c); /* From local to intermediate. */
  621.         if (rxo) c = (*rxo)(c); /* From intermediate to remote. */
  622. #else
  623.         if (inesc == ES_NORMAL) { /* If not inside escape seq.. */
  624.             /* Translate character sets */
  625.             if (sxo) c = (*sxo)(c); /* Local to intermediate. */
  626.             if (rxo) c = (*rxo)(c); /* Intermediate to remote. */
  627.         }
  628.         if (skipesc) chkaes(c); /* Check escape sequence status */
  629. #endif /* SKIPESC */
  630. #endif /* NOCSETS */
  631. /*
  632.  If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  633.  handle shifting here.
  634. */
  635.         if (sosi) {             /* Shift-In/Out selected? */
  636.             if (cmask == 0177) { /* In 7-bit environment? */
  637.             if (c & 0200) {          /* 8-bit character? */
  638.                 if (outshift == 0) { /* If not shifted, */
  639.                 if (ttoc(dopar(SO)) < 0) { /* shift. */
  640.                     active = 0;
  641.                     continue;
  642.                 }
  643.                 outshift = 1;
  644.                 }
  645.             } else {
  646.                 if (outshift == 1) { /* 7-bit character */
  647.                 if (ttoc(dopar(SI)) < 0) { /* If shifted, */
  648.                     active = 0;
  649.                     continue;
  650.                 }
  651.                 outshift = 0; /* unshift. */
  652.                 }
  653.             }
  654.             }
  655.             if (c == SO) outshift = 1;    /* User typed SO */
  656.             if (c == SI) outshift = 0;    /* User typed SI */
  657.         }
  658.         c &= cmask;        /* Apply Kermit-to-host mask now. */
  659.  
  660. /* If we have a CR, handle CR/CRLF mapping... */
  661.  
  662.         if (c == '\015') {
  663.             if (tnlm        /* TERMINAL NEWLINE ON */
  664. #ifdef TNCODE                /* And for TELNET... */
  665.             || (network && ttnproto == NP_TELNET)
  666. #endif /* TNCODE */
  667.             ) {
  668.             ttoc(dopar('\015'));        /* Send CR */
  669.             if (duplex) conoc('\015'); /* Maybe echo CR */
  670. #ifdef TNCODE
  671.             if (network && !tn_nlm && ttnproto == NP_TELNET)
  672.               c = '\0';    /* Stuff NUL */
  673.             else
  674. #endif /* TNCODE */
  675.               c = '\012';    /* Stuff LF */
  676.             csave = c;
  677.             }
  678.             }            /* Now process the LF or NUL... */
  679. #ifdef TNCODE
  680. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  681.         else
  682.           if (c == IAC && network && ttnproto == NP_TELNET) {
  683.                           /* Send one copy now */
  684.               ttoc(IAC);    /* and the other one just below. */
  685.           }
  686. #endif /* TNCODE */
  687.         if (ttoc(dopar(c)) < 0) { /* Now send the character. */
  688.             active = 0;
  689.             continue;
  690.         }
  691.         if (duplex) {        /* Half duplex? */
  692.             if (debses)        /* Yes, echo locally */
  693.               conol(dbchr(csave)); /* in appropriate mode */
  694.             else
  695.               conoc(csave);
  696.             if (seslog) zchout(ZSFILE,c); /* And maybe log it. */
  697.         }            
  698.         }
  699.     } else {
  700. /*
  701.   Character from comm. line
  702. */
  703. #ifdef TNCODE
  704.         /* Handle telnet options */
  705.         if (network && nettype == NP_TELNET && ((c & 0xff) == IAC)) {
  706.         ckcputf();        /* Dump output buffer */
  707.         if ((x = tn_doop(c & 0xff, duplex, ckcgetc)) == -1 && !quiet)
  708.           printf("\r\nCommunications disconnect ");
  709.         if (x == 1) duplex = 1;    /* Change duplex if necessary. */
  710.         if (x == 2) duplex = 0;
  711.         continue;        /* Negotiation OK, go get next char. */
  712.         }
  713. #endif /* TNCODE */
  714.         if (debses) {        /* Output character to screen */
  715.         char *s;        /* Debugging display... */
  716.         s = dbchr(c);
  717.         while (*s)
  718.           ckcputc(*s++);
  719.         } else {            /* or regular... */
  720.         c &= cmask;        /* Do first masking */
  721.         if (sosi) {        /* Handle SI/SO */
  722.             if (c == SO) {    /* Shift Out */
  723.             inshift = 1;
  724.             continue;
  725.             } else if (c == SI) { /* Shift In */
  726.             inshift = 0;
  727.             continue;
  728.             }
  729.             if (inshift) c |= 0200;
  730.         }
  731. #ifndef NOCSETS
  732. #ifndef SKIPESC
  733.         if (sxi) c = (*sxi)(c);    /* Xlate char sets */
  734.         if (rxi) c = (*rxi)(c);
  735. #else
  736.         if (inesc == ES_NORMAL) {
  737.             if (sxi) c = (*sxi)(c);
  738.             if (rxi) c = (*rxi)(c);
  739.         }
  740.         if (skipesc) chkaes(c); /* Esc seq status */
  741. #endif /* SKIPESC */
  742. #endif /* NOCSETS */
  743.         c &= cmdmsk;        /* Apply mask */
  744.         if (c == CR && tt_crd) { /* SET TERM CR-DISPLAY CRLF ? */
  745.             ckcputc(c);         /* Yes, output CR */
  746.             if (seslog) zchout(ZSFILE,c);
  747.             c = LF;             /* and insert a linefeed */
  748.         }
  749.         ckcputc(c);        /* Put it on the screen. */
  750.         if (seslog) zchout(ZSFILE,c); /* If logging, log it. */
  751.         }
  752.     }
  753.     } while (active);
  754.     cancio();
  755.     conres();
  756.     if (!quiet)
  757.       printf("\r\n(Back at %s)\r\n",
  758.          *myhost ? myhost : "local VMS system");
  759.     what = W_NOTHING;
  760. #ifndef NOCSETS
  761.     language = langsv;            /* Restore language */
  762. #endif /* NOCSETS */
  763.     return(1);
  764. }
  765.  
  766. /*  H C O N N E  --  Give help message for connect.  */
  767.  
  768. VOID
  769. hconne() {
  770.     int c;
  771.     static char *hlpmsg[] = {
  772. "\n",
  773. "  C to return to C-Kermit prompt,   H to hangup and close the connection,\n",
  774. "  B to send a BREAK,                L to send a Long BREAK,\n",
  775. #ifdef TNCODE
  776. "  A to send TELNET Are You There,   I to send TELNET Interrupt,\n",
  777. #endif /* TNCODE */
  778. "  0 (zero) to send a null,          X to send an XON,\n",
  779. #ifdef NOPUSH
  780. "  S for status of connection,       ? for this message, or:\n",
  781. #else
  782. "  @ to enter DCL,                   S for status of connection,\n",
  783. "  ? for this message, or:\n",
  784. #endif /* NOPUSH */
  785. "  \\ to begin a backslash escape:\n",
  786. "    \\nnn  (decimal character code)\n",
  787. "    \\Onnn (octal character code)\n",
  788. "    \\Xhh  (hexadecimal character code)\n",
  789. "    Terminate with carriage return.\n\n",
  790. " Type the escape character again to send the escape character, or\n",
  791. " press the space-bar to resume the CONNECT command.\n\n",
  792. "" };
  793. /*
  794.   Need to save term characteristics/ allow disable binary mode
  795.   print message, get text and then restore previous state.
  796. */
  797.     conol("\r\nPress C to return to ");
  798.     conol(*myhost ? myhost : "the C-Kermit prompt");
  799.     conoll(", or:");
  800.     conola(hlpmsg);            /* Print the help message. */
  801.     conol("Command>");            /* Prompt for command. */
  802.     c = coninc(0) & 0x7f;
  803.     conoc(c);                /* Echo it. */
  804.     if (c != CMDQ)
  805.       conoll("");
  806.     doesc(c);
  807. }
  808.  
  809. /*  D O E S C  --  Process an escape character argument  */
  810.  
  811. VOID
  812. #ifdef CK_ANSIC
  813. doesc(register unsigned char c)
  814. #else
  815. doesc(c) register unsigned char c;
  816. #endif /* CK_ANSIC */
  817. /* doesc() */ {
  818.     int d;
  819.     char sbuf[35];
  820.  
  821.     c &= 0177;                /* Mask off 8th bit */
  822.  
  823.     if (c == escape) {            /* If it's the escape character, */
  824.         d = dopar(c);            /* just send it. */
  825.         ttoc(d);
  826.     return;
  827.     }
  828.     if (isupper(c)) c = tolower(c);    /* Convert to lowercase letter. */
  829.     if (iscntrl(c)) c += 'a' - '\001';
  830.  
  831.     switch (c) {            /* Take requested action. */
  832.       case 'b':
  833.     ttsndb();            /* Send a BREAK signal */
  834.     break;
  835. #ifdef TNCODE
  836.       case 'i':                /* Send TELNET interrupt */
  837. #ifndef IP
  838. #define IP 244
  839. #endif /* IP */
  840.     if (network && ttnproto == NP_TELNET) { /* TELNET */
  841.         CHAR temp[3];
  842.         temp[0] = IAC;        /* I Am a Command */
  843.         temp[1] = IP;        /* Interrupt Process */
  844.         temp[2] = NUL;
  845.         ttol((CHAR *)temp,2);
  846.     } else conoc(BEL);
  847.     break;
  848.       case 'a':                /* "Are You There?" */
  849.       case '\01':
  850. #ifndef AYT
  851. #define AYT 246
  852. #endif /* AYT */
  853.     if (network && ttnproto == NP_TELNET) {
  854.         CHAR temp[3];
  855.         temp[0] = IAC;        /* I Am a Command */
  856.         temp[1] = AYT;        /* Are You There? */
  857.         temp[2] = NUL;
  858.         ttol((CHAR *)temp,2);
  859.     } else conoc(BEL);
  860. #endif /* TNCODE */
  861.     break;
  862.       case 'c':                /* Return to prompt */
  863.     active = 0;
  864.     conol("\r\n");
  865.     break;
  866.       case 'h':                /* Hang up the connection */
  867. #ifndef NODIAL
  868.     if (mdmhup() < 1)        /* Try via modem first, otherwise */
  869. #endif /* NODIAL */
  870.       tthang();            /* the old-fashioned way. */
  871.     conol("\r\nHanging up ");
  872.     break;
  873.       case 'l':                /* Send a Long BREAK signal */
  874.     ttsndlb();
  875.     break;
  876.       case 's':                /* Status */
  877.     sprintf(sbuf,"Connected thru %s",ttname);
  878.     conol(sbuf);
  879.     sprintf(sbuf,", speed: %d",speed);
  880.     conol(sbuf);
  881.     if (seslog) {
  882.         sprintf(sbuf,", logging file: %s",sesfil);
  883.         conol(sbuf);
  884.     }
  885.     conoll("");
  886.     break;
  887. #ifndef NOPUSH
  888.       case '!':
  889.       case '@':
  890.     conres();            /* Put console back to normal */
  891.     zshcmd("");            /* Start DCL. */
  892.     if (conbin(escape) < 0) {
  893.         printf("Error returning to remote session\n");
  894.         active = 0;
  895.     }
  896.     return;
  897. #endif /* NOPUSH */
  898.  
  899.       case 'x':                /* XON */
  900. /*
  901.   NOTE: Here we should also issue QIO's to clear XOFF deadlock.
  902. */
  903.     ttoc(dopar(XON));
  904.     break;
  905.       case '?':                /* Give Help */
  906.     hconne();
  907.     break;
  908.       case '0':                /* Send a NULL */
  909.     c = '\0';
  910.     d = dopar(c);
  911.     ttoc(d);
  912.     break;
  913.       case SP:                /* Ignore space */
  914.     break;
  915.       default:
  916.     if (c == CMDQ) {        /* Backslash escape */
  917.         int x;
  918.         kbp = kbuf;
  919.         *kbp++ = c;
  920.         while (((c = (coninc(0) & cmdmsk)) != '\r') && (c != '\n'))
  921.           *kbp++ = c;
  922.         *kbp = NUL; kbp = kbuf;
  923.         x = xxesc(&kbp);
  924.         if (x >= 0) {
  925.         c = dopar(x);
  926.         ttoc(c);
  927.         return;
  928.         } else {
  929.         conoc(BEL);
  930.         return;
  931.         }
  932.     }
  933.     conoc(BEL); return;        /* Invalid esc arg, beep */
  934.     }
  935. }
  936.